<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/24
 * Time: 15:22
 */
 namespace Modules\Admin\Http\Controllers;

use App\Http\Requests\RequestAdmins;
use HiCommon\Service\AdminUserService;
use HiCommon\Service\RoleService;
use Illuminate\Support\Facades\Request;

class AdminsController extends BaseController
{


    /**
     *  管理后台用户 列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function index()
    {
        try {
            $arr_search = Request::input();
            $res = AdminUserService::api_search_list($arr_search);
            echo_error($res);
            return view('admin::admins.index', $res['data']);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 添加 管理后台用户 页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function create()
    {
        try {
            $res = RoleService::get_all_roles_for_admin();
            echo_error($res);

            $arr_data = [
              'arr_role'=>$res['data'],
              'arr_sex'=>[0=>'保密',1=>'男',2=>'女'],
            ];
            return view('admin::admins.create',$arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }

    }

    /**
     * 添加 管理后台用户 操作
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function store(RequestAdmins $request)
    {
        try {
            $arr_input = $request->input();
            shiwu_start();
            $res = AdminUserService::api_add_one($arr_input);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     * 修改 管理后台用户 页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit()
    {
        try {
            $n_id = Request::get('id',0);
            $arr_data['arr_sex'] = [0=>'保密',1=>'男',2=>'女'];
            $res = RoleService::get_all_roles_for_admin();
            echo_error($res);
            $arr_data['arr_role'] = $res['data'];

            $res = AdminUserService::api_get_one($n_id);
            echo_error($res);
            $arr_data['arr_data'] = $res['data'];

            return view('admin::admins.edit',$arr_data);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     * 修改 管理后台用户 页面操作
     *
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function update(RequestAdmins $request)
    {
        try {
            $arr_input = $request->input();
            return AdminUserService::api_edit_one($arr_input);
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }


    /**
     * 更改 管理后台用户 状态
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_state()
    {
        try {
            $arr_input = Request::input();
            shiwu_start();
            $res = AdminUserService::api_change_one_state($arr_input);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }

    /**
     * 删除 管理后台用户
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete()
    {
        try {
            $n_id = (int)Request::get('id',0);
            shiwu_start();
            $res = AdminUserService::api_delete_one($n_id);
            shiwu_end($res);
            return $res;
        }
        catch (\Exception $exception) {
            return hello_error('失败-'.$exception->getMessage(),[],$exception->getMessage());
        }
    }
}